package com.rapida.framwork;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Objects;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.rapida.app.BaseActivity;

/**
 * Created by mm on 14-12-17.
 */
public abstract class RapidActivity extends BaseActivity {
    private UIViewController mUIController;
    protected abstract boolean didFinishLaunchingWithOptions();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (didFinishLaunchingWithOptions() && mUIController != null) {
            registerViewEvent(mUIController, getWindow().getDecorView());
            mUIController.viewDidLoad();
        }
    }

    public void setViewController(UIViewController uiController) {
        mUIController = uiController;
        registerViewLayout(mUIController);
    }

    private void registerViewLayout(Object classObj) {
        IVOutlet layout = classObj.getClass().getAnnotation(IVOutlet.class);
        int layoutId = layout.id();
        setContentView(layoutId);
    }

	private void registerViewEvent(Object classObj, View rootView) {
		Field[] fields = classObj.getClass().getDeclaredFields();
		if (fields != null && fields.length > 0) {
			for (Field field : fields) {
				try {
					field.setAccessible(true);
					if (field.get(classObj) != null) {
						continue;
					}
					IBOutlet viewreg = field.getAnnotation(IBOutlet.class);
					int viewId = viewreg.id();
					field.set(classObj, rootView.findViewById(viewId));
					/*Object obj = field.get(classObj);
					if (viewreg.click()) {
						if (obj instanceof View) {
							((View) obj).setOnClickListener(this);
						}
					}
					if (viewreg.longClick()) {
						if (obj instanceof View) {
							((View) obj).setOnLongClickListener(this);
						}
					}
					if (viewreg.itemClick()) {
						if (obj instanceof ListView) {
							((ListView) obj).setOnItemClickListener(this);
						}
					}*/
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
